package com.iengoo.inductionms.service.impl;

import com.iengoo.inductionms.entity.Department;
import com.iengoo.inductionms.entity.Employee;
import com.iengoo.inductionms.mapper.DepartmentMapper;
import com.iengoo.inductionms.mapper.EmployeeMapper;
import com.iengoo.inductionms.model.Condition;
import com.iengoo.inductionms.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief
 * @email 122162005@qq.com
 * @date 2019-03-08 21:15
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    //查询资料报道完成而没有进行信息录入的员工
    @Override
    public List<Employee> findAll() {
        return employeeMapper.findAll();
    }

    @Override
    public List<Employee> query(Condition condition) {
        return employeeMapper.query(condition);
    }

    //新建账号
    @Override
    public void create(Employee employee) {
        //获取员工职务
        String employeePost = employee.getPost();
        //获取员工所在部门简称
        String employeeDepartment = employee.getDepartment();
        //根据部门简称查询部门编号,部门名称和员工总数
        Department queryDepartmentNumAndCode = departmentMapper.queryDepartmentNumAndCode(employeeDepartment);
        //部门人数
        Integer departmentNum = queryDepartmentNumAndCode.getDepartmentNum();
        //判断:1.如果该员工不是经理更新该员工所在部门的人数统计,在原人数上加一;
        //     2.如果该员工是经理,部门人数不变
        if (employeePost != null && !employeePost.equals("经理")) {
            queryDepartmentNumAndCode.setDepartmentNum(departmentNum + 1);
            //在数据库中更新员工所在部门的部门人数
            departmentMapper.updateByCode(queryDepartmentNumAndCode);
        }
        //设置新增员工的状态为1;
        employee.setType("1");
        employeeMapper.insertSelective(employee);
    }
    //资料报道页面修改员工基本信息
    @Override
    public void update(Employee employee) {
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp updateTime = new Timestamp(currentTimeMillis);
        employee.setUpdateTime(updateTime);
        employeeMapper.update(employee);
    }
    //根据员工id删除(冻结)员工,将员工的状态改为2,更新时间改为当前时间;
    @Override
    public void frozenAccounts(Employee employee) {
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp updateTime = new Timestamp(currentTimeMillis);
        employee.setUpdateTime(updateTime);
        employee.setType("2");
        employeeMapper.frozenAccounts(employee);
    }
    //根据员工填写的信息进行修改
        @Override
        public void updateInformation(Employee employee) {
            long currentTimeMillis = System.currentTimeMillis();
            Timestamp updateTime = new Timestamp(currentTimeMillis);
            employee.setUpdateTime(updateTime);
            employeeMapper.updateInformation(employee);
    }
    //判断员工工号是否重复
    @Override
    public List<Employee> queryByEmployeeId(String employeeId) {
        List<Employee> employeeList = employeeMapper.queryByEmployeeId(employeeId);
        return employeeList;
    }

    @Override
    public Employee findById(int id) {
        return employeeMapper.findById(id);
    }
}
